home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Installer SDK 1.2.3 / Upgrader 1.2.3 & Engines / Upgrader 1.2.3 / Plug-in Examples / Common Files / Editor Utilities / LGroupBox.cp < prev    next >
Encoding:
Text File  |  1997-06-10  |  6.1 KB  |  246 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    LGroupBox.cp               ©1995-1996 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4.  
  5. #ifdef PowerPlant_PCH
  6. #include PowerPlant_PCH
  7. #endif
  8.  
  9. #include <LGroupBox.h>
  10. #include <UDrawingState.h>
  11. #include <UDrawingUtils.h>
  12. #include <UTextTraits.h>
  13.  
  14.  
  15. // ———————————————————————————————————————————————————————————————————————————
  16. //        • CreateGroupBoxStream
  17. // ———————————————————————————————————————————————————————————————————————————
  18. //    Create a new GroupBox object from the data in a Stream
  19.  
  20. LGroupBox*
  21. LGroupBox::CreateGroupBoxStream(
  22.     LStream* inStream)
  23. {
  24.     return new LGroupBox(inStream);
  25. }
  26.  
  27.  
  28. // ———————————————————————————————————————————————————————————————————————————
  29. //        • LGroupBox
  30. // ———————————————————————————————————————————————————————————————————————————
  31. //    Default Constructor
  32.  
  33. LGroupBox::LGroupBox()
  34. {
  35.     mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x8000;
  36. }
  37.  
  38.  
  39. // ———————————————————————————————————————————————————————————————————————————
  40. //        • LGroupBox(const LGroupBox&)
  41. // ———————————————————————————————————————————————————————————————————————————
  42. //    Copy Constructor
  43.  
  44. LGroupBox::LGroupBox(
  45.     const LGroupBox        &inGroupBox)
  46.         : LCaption(inGroupBox)
  47. {
  48.     mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x8000;
  49. }
  50.  
  51.  
  52. // ———————————————————————————————————————————————————————————————————————————
  53. //        • LGroupBox(SPaneInfo&, Str255, ResIDT)
  54. // ———————————————————————————————————————————————————————————————————————————
  55. //    Construct from parameters. Use this constructor to create a GroupBox
  56. //    from runtime data.
  57.  
  58. LGroupBox::LGroupBox(
  59.     const SPaneInfo&    inPaneInfo,
  60.     Str255                inString,
  61.     ResIDT                inTextTraitsID)
  62.         : LCaption(inPaneInfo, inString, inTextTraitsID)
  63. {
  64.     mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x8000;
  65. }
  66.  
  67.  
  68. // ———————————————————————————————————————————————————————————————————————————
  69. //        • LGroupBox(LStream*)
  70. // ———————————————————————————————————————————————————————————————————————————
  71. //    Construct from data in a Stream
  72.  
  73. LGroupBox::LGroupBox(
  74.     LStream *inStream)
  75.         : LCaption(inStream)
  76. {
  77.     mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x2000;
  78. }
  79.  
  80.  
  81. // ———————————————————————————————————————————————————————————————————————————
  82. //        • ~LGroupBox
  83. // ———————————————————————————————————————————————————————————————————————————
  84. //    Destructor
  85.  
  86. LGroupBox::~LGroupBox()
  87. {
  88. }
  89.  
  90.  
  91. // ———————————————————————————————————————————————————————————————————————————
  92. //        • DrawSelf
  93. // ———————————————————————————————————————————————————————————————————————————
  94. //    Draw GroupBox
  95. //
  96. //  We draw in two stages. First we draw the frame rectangle,
  97. //  minus the area where the title text is drawn; then we draw the
  98. //  title text. In order to draw without any flashing, we find the
  99. //  frame of the text box and clip it out of the drawing region.
  100. //
  101. void
  102. LGroupBox::DrawSelf()
  103. {
  104.     Rect textFrame;
  105.     CalcTextBoxFrame(textFrame);
  106.  
  107.     RgnHandle    newClip = ::NewRgn();
  108.     ThrowIfNil_(newClip);
  109.     ::GetClip(newClip);
  110.  
  111.     RgnHandle textRgn = ::NewRgn();
  112.     ThrowIfNil_(textRgn);
  113.     ::RectRgn(textRgn, &textFrame);
  114.  
  115.     ::DiffRgn(newClip, textRgn, newClip);
  116.  
  117.     {
  118.         StClipRgnState clip(newClip);
  119.         
  120.         Rect borderFrame;
  121.         CalcLocalFrameRect(borderFrame);
  122.         if (textFrame.right > textFrame.left)
  123.             borderFrame.top += (textFrame.bottom - textFrame.top) >> 1;
  124.         
  125.         DrawBorder(borderFrame);
  126.     }
  127.  
  128.     ::PenNormal();    
  129.     DrawText(textFrame);
  130.  
  131.     ::DisposeRgn (newClip);
  132.     ::DisposeRgn (textRgn);
  133. }
  134.  
  135.  
  136. // ———————————————————————————————————————————————————————————————————————————
  137. //        • DrawText
  138. // ———————————————————————————————————————————————————————————————————————————
  139. //    Draw the text given the placement chosen by CalcTextBoxFrame
  140.  
  141. void
  142. LGroupBox::DrawText(
  143.     const Rect        &inRect)
  144. {
  145.         // Retrieve info about font again.
  146.  
  147.     Int16 just = UTextTraits::SetPortTextTraits(mTxtrID);
  148.     FontInfo fInfo;
  149.     ::GetFontInfo(&fInfo);
  150.     
  151.         // Set background color
  152.     
  153.     RGBColor    textColor;            // Text has its own foreground color
  154.     ::GetForeColor(&textColor);
  155.     
  156.     ApplyForeAndBackColors();
  157.     ::RGBForeColor(&textColor);
  158.     
  159.     ::MoveTo(inRect.left + 3, inRect.top + fInfo.ascent + (fInfo.leading >> 1));
  160.     ::DrawString(mText);
  161.  
  162. }
  163.  
  164.  
  165. // ———————————————————————————————————————————————————————————————————————————
  166. //        • DrawBorder
  167. // ———————————————————————————————————————————————————————————————————————————
  168. //    Draw border around the GroupBox
  169.  
  170. void
  171. LGroupBox::DrawBorder(
  172.     const Rect    &inRect)
  173. {
  174.     StDeviceLoop devices(inRect);
  175.     Int16 depth;
  176.     
  177.     ::PenNormal();
  178.     
  179.     while (devices.NextDepth(depth)) {
  180.         StColorPenState savePenState;        // Will save and restore pen state
  181.         if (depth > 2) {
  182.             ::RGBForeColor(&mFrameColor);
  183.         } else {
  184.             ::PenPat(&UQDGlobals::GetQDGlobals()->gray);
  185.         }
  186.         ::FrameRect(&inRect);
  187.     }
  188. }
  189.  
  190.  
  191. // ———————————————————————————————————————————————————————————————————————————
  192. //        • CalcTextBoxFrame
  193. // ———————————————————————————————————————————————————————————————————————————
  194. //    Decides where the text will be drawn, but does not draw the text
  195.  
  196. void
  197. LGroupBox::CalcTextBoxFrame(
  198.     Rect    &outRect)
  199. {
  200.     // Find the edges of the pane.
  201.     
  202.     CalcLocalFrameRect(outRect);
  203.  
  204.  
  205.     // If text is empty, return an empty rectangle.
  206.     
  207.     if (mText[0] == 0) {
  208.         outRect.right = outRect.left;
  209.         outRect.bottom = outRect.top;
  210.         return;
  211.     }
  212.  
  213.  
  214.     // We have some text, find its size and place it.
  215.     // This version is pretty naive; it assumes the text will
  216.     // fit comfortably on a single line.
  217.     
  218.     Int16 just = UTextTraits::SetPortTextTraits(mTxtrID);
  219.     Int16 strSize = ::StringWidth(mText) + 6;            // 3 pixels slop on either side
  220.     Int16 frameWidth = outRect.right - outRect.left;
  221.  
  222.     FontInfo fInfo;                        // vertical placement is always at top
  223.     ::GetFontInfo(&fInfo);
  224.     outRect.bottom = outRect.top + fInfo.ascent + fInfo.descent + fInfo.leading;
  225.  
  226.     switch (just) {                        // horizontal placement depends on Txtr
  227.  
  228.         case teCenter:
  229.             outRect.left += (frameWidth - strSize) >> 1;
  230.             outRect.right = outRect.left + strSize;
  231.             break;
  232.             
  233.         case teFlushRight:
  234.             outRect.right -= 6;
  235.             outRect.left = outRect.right - strSize;
  236.             break;
  237.         
  238.         case teFlushDefault:
  239.         case teFlushLeft:
  240.         default:
  241.             outRect.left += 6;
  242.             outRect.right = outRect.left + strSize;
  243.             break;
  244.     }
  245. }
  246.